package com.example.imbmq80ai.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsOperations;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.*;

import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.xml.soap.Text;

/**
 * @program: seckill
 * @description: 消息生产者
 * @author: Xiaos
 * @create: 2020-10-13 09:54
 **/
@RestController
@RequestMapping("/send")
@Slf4j
public class Sender {
    @Autowired
    JmsOperations jmsOperations;

    @Autowired
    JmsTemplate jmsTemplate;

    @GetMapping("/msg/mq")
    public void sendMq(@RequestParam String msg) {
        log.info("获得报文并开始向mq发送："+msg);

        jmsTemplate.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        jmsTemplate.setExplicitQosEnabled(true);
        jmsTemplate.send("QUEUE1", session -> {
            TextMessage m = session.createTextMessage();
            m.setText(msg);
            m.setJMSCorrelationID("1111");
            return m;
        });

//        Message message = jmsTemplate.receiveSelected("QUEUE1", "JMSCorrelationID='2222'");
//        System.err.println("---->" + message.toString());

        log.info("======推送mq报文完成======");
    }

}

